<template>
  <div class="app">
    <div class="search-div-input">
      <el-input
        clearable
        placeholder="请输入集团名称"
        class="search-div-input-input"
        v-model="filterDataOfSearch.groupName"
      />
      <el-input
        clearable
        placeholder="请输入供应商名称"
        class="search-div-input-input"
        v-model="filterDataOfSearch.supplierName"
      />
      <el-button type="primary" @click="GoToSearch()">搜索</el-button>
      <el-button type="primary" @click="GoToAdd()">新增</el-button>
    </div>
    <template>
      <el-table :data="tableData" border style="width: 100%">
        <el-table-column
          fixed
          prop="groupName"
          label="集团名称"
        ></el-table-column>
        <el-table-column
          prop="supplierName"
          label="供应商名称"
        ></el-table-column>
        <el-table-column prop="effectTime" label="生效日期"></el-table-column>
        <el-table-column prop="endTime" label="失效日期"></el-table-column>
        <el-table-column prop="statu" label="状态">
          <template slot-scope="scope">
            {{ scope.row.statu == 1 ? '正常' : '禁用' }}
          </template>
        </el-table-column>
        <el-table-column fixed="right" label="操作">
          <template slot-scope="scope">
            <el-button @click="GOToCheck(scope.row)" type="text" size="small">
              查看
            </el-button>
            <el-button type="text" size="small" @click="GoToEdit(scope.row)">
              编辑
            </el-button>
            <el-button
              type="text"
              size="small"
              @click="GoToCancel(scope.row)"
              v-show="scope.row.statu == 1 ? true : false"
            >
              注销
            </el-button>
            <el-button
              type="text"
              size="small"
              @click="GoToActivate(scope.row)"
              v-show="scope.row.statu == 0? true : false"
            >
              激活
            </el-button>
          </template>
        </el-table-column>
      </el-table>
    </template>
    <el-pagination
      background
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page="filterData.current"
      :page-size="filterData.pagesize"
      layout="total, prev, pager, next"
      :total="totals"
    ></el-pagination>
  </div>
</template>
 
<script>
import { getPermissionsList } from '@/api/goodsPermissions'
import { getPermissionsListEdit } from '@/api/goodsPermissions'

export default {
  data() {
    return {
      tableData: [],
      filterData: {
        current: 1,
        pagesize: 10,
      },
      filterDataOfSearch: {
        groupName: '',
        supplierName: '',
      },
      //分页。
      totals: 0,
    }
  },
  created() {
    this.triggerGetPermissionsList()
  },
  methods: {
    //页面数据查询。
    async triggerGetPermissionsList() {
      const res = await getPermissionsList(this.filterData)
      this.tableData = res.data.list
      this.totals = res.data.total
    },
    async GoToSearch() {
      const res = await getPermissionsList(this.filterDataOfSearch)
      this.tableData = res.data.list
      this.totals = res.data.total
    },
    //跳转新增页面。
    GoToAdd() {
      this.$router.push({
        path: '/goods/goodsPermissionsAdd',
        query: { title: 'add' },
      })
    },
    //跳转编辑页面。
    GoToEdit(editData) {
      this.$router.push({
        path: '/goods/goodsPermissionsAdd',
        query: { title: 'edit', editData },
      })
    },
    //跳转查看页面。
    GOToCheck(checkData) {
      this.$router.push({
        path: '/goods/goodsPermissionsAdd',
        query: { title: 'check', checkData },
      })
    },
    //注销。
    GoToCancel(data) {
      this.$confirm('是否确定此操作?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning',
      })
        .then(() => {
          this.triggerGetPermissionsListEdit(data)
        })
        .catch(() => {
          this.$message.info('已取消此操作')
        })
    },
    //注销接口。
    async triggerGetPermissionsListEdit(data) {
      let cancelData = {
        id: data.id,
        statu: 0,
      }
      const res = await getPermissionsListEdit(cancelData)
      if (res.status == 200) {
        this.$message.success('操作成功')
        this.triggerGetPermissionsList()
      } else {
        this.$message.error('操作失败')
      }
    },
    //激活。
    async GoToActivate(datas) {
      this.$confirm('是否确定此操作?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning',
      })
        .then(() => {
          this.triggerGetPermissionsListEdit02(datas)
        })
        .catch(() => {
          this.$message.info('已取消此操作')
        })
    },
    //激活接口。
    async triggerGetPermissionsListEdit02(datas) {
      let activateData = {
        id: datas.id,
        statu: 1,
      }
      const res = await getPermissionsListEdit(activateData)
      if (res.status == 200) {
        this.$message.success('操作成功')
        this.triggerGetPermissionsList()
      } else {
        this.$message.error('操作失败')
      }
    },
    //分页方法。
    handleSizeChange(val) {
      this.filterData.pagesize = val
      this.triggerGetPermissionsList()
    },
    handleCurrentChange(val) {
      this.filterData.current = val
      this.triggerGetPermissionsList()
    },
  },
}
</script>
 
<style scoped lang="scss">
.app {
  padding: 15px;
  .search-div-input {
    display: flex;
    margin-bottom: 15px;
    .search-div-input-input {
      margin-right: 15px;
      width: 220px;
    }
  }
}
</style>